package cn.fighter3.service.impl;

import cn.fighter3.entity.node.*;
import cn.fighter3.entity.relationship.*;
import cn.fighter3.service.node.*;
import cn.fighter3.service.relationship.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class PeopleService {
    @Autowired
    PeopleDao peopleDao;

    @Autowired
    CnbookDao cnbookDao;
    @Autowired
    FgbookDao fgbookDao;
    @Autowired
    MscbookDao mscbookDao;
    @Autowired
    MulbookDao mulbookDao;
    @Autowired
    PapbookDao papbookDao;
    @Autowired
    PerbookDao perbookDao;

    //cypher查询
    public List<Object> findByCypher(String cypher){
        System.out.println(cypher);
        return peopleDao.findByCypher(cypher);
    }

    public List<People> findByAuthorName(String name){
        return peopleDao.findByAuthorName(name);
    }

    public List<People> findAll(){
        return (List<People>) peopleDao.findAll();
    }

    public List<People> all(){


        return  peopleDao.all();
    }
    public List<People> find7(String name){return peopleDao.findByAuthorNameLike(name);}

//    public Set<Cnbook> getBooksByRelationship(People people,String rel){
//        if(rel.equals("authorof")){
//            return people.getAuthorCnbook();
//        }
//        else{
//            return null;
//        }
//    }

    public Set<CustomNode> getBooksByRelationship(People people, String rel){
        Set<CustomNode> res = new HashSet<>();
        if(rel.equals("authorof")){
            Set<Cnbook> bk1 = people.getAuthorCnbook();
            Set<Fgbook> bk2 = people.getAuthorFgbook();

            Set<Mscbook> bk3 = people.getAuthorMscbook();
            Set<Mulbook> bk4 = people.getAuthorMulbook();
            Set<Perbook> bk5 = people.getAuthorPerbook();
            Set<Papbook> bk6 = people.getAuthorPapbook();

            for (Cnbook bk : bk1){
                res.add(new CustomNode(bk.getTitle(),2,bk,0));//cnbookDao.countcn(bk.getBookId())
            }
            for (Fgbook bk : bk2){
                res.add(new CustomNode(bk.getTitle(),3,bk,0));//fgbookDao.countfg(bk.getBookId())
            }

            for (Mscbook bk : bk3){
                res.add(new CustomNode(bk.getTitle(),4,bk,0));//mscbookDao.countmsc(bk.getBookId())
            }
            for (Mulbook bk : bk4){
                res.add(new CustomNode(bk.getTitle(),5,bk,0));//mulbookDao.countmul(bk.getBookId())
            }
            for (Perbook bk : bk5){
                res.add(new CustomNode(bk.getTitle(),7,bk,0));//perbookDao.countper(bk.getBookId())
            }
            for (Papbook bk : bk6){
                res.add(new CustomNode(bk.getTitle(),6,bk,0));//papbookDao.countpap(bk.getBookId())
            }

            //System.out.println(res);
            return res;
            //return people.getAuthorCnbook();
        }
        else if (rel.equals("lend")){
            Set<Cnbook> bk1 = people.getLendCnbook();
            Set<Fgbook> bk2 = people.getLendFgbook();

            Set<Mscbook> bk3 = people.getLendMscbook();
            Set<Mulbook> bk4 = people.getLendMulbook();
            Set<Perbook> bk5 = people.getLendPerbook();
            Set<Papbook> bk6 = people.getLendPapbook();

            for (Cnbook bk : bk1){
                res.add(new CustomNode(bk.getTitle(),2,bk,0));//cnbookDao.countcn(bk.getBookId())
            }
            for (Fgbook bk : bk2){
                res.add(new CustomNode(bk.getTitle(),3,bk,0));//fgbookDao.countfg(bk.getBookId())
            }

            for (Mscbook bk : bk3){
                res.add(new CustomNode(bk.getTitle(),4,bk,0));//mscbookDao.countmsc(bk.getBookId())
            }
            for (Mulbook bk : bk4){
                res.add(new CustomNode(bk.getTitle(),5,bk,0));//mulbookDao.countmul(bk.getBookId())
            }
            for (Perbook bk : bk5){
                res.add(new CustomNode(bk.getTitle(),7,bk,0));//perbookDao.countper(bk.getBookId())
            }
            for (Papbook bk : bk6){
                res.add(new CustomNode(bk.getTitle(),6,bk,0));//papbookDao.countpap(bk.getBookId())
            }

            //System.out.println(res);
            return res;
        }
        else {
            return null;
        }
    }

    //增加
//    public People create(People people) {
//        return peopleDao.save(people);
//    }
    public People create(String name,String id){
        People people = new People(name,id);
        return peopleDao.save(people);
    }

//    public People save(People people){
//        return peopleDao.save(people);
//    }

    public People save(People people){
        //return peopleDao.save(people);
        return peopleDao.save(people);
    }

    //删除
    public void deleteByAuthorId(String id) {
        //peopleDao.deleteByAuthorId(id);
        peopleDao.deleteById(id);
    }

    public void deleteByAuthorName(String name){
        peopleDao.deleteByAuthorName(name);
    }


    //查询
    public Optional<People> findByAuthorId(String id){
        return peopleDao.findByAuthorId(id);
    }

    //修改
//    public People updateByNode(People people){
//        return peopleDao.updateByNode(people);
//    }

    public People updateByNode(String id,String name){
        return peopleDao.updateByNode(id,name);
    }

    //由书找人--Authorof
    public List<People> findByfromid1(String id){return peopleDao.findByfromid(id);}
    //由书找人--Lend
    public List<People> findByfromid11(String id){return peopleDao.findByfromid11(id);}
    //关系数量
    public Integer count(String id){return peopleDao.countpeo(id);}





    @Autowired
    AuthorcnDao authorcnDao;
    public Authorof1 addAuthor1(String fromId, String toId){
        return authorcnDao.addAuthor(fromId,toId);
    }
    public void deleteByNodeId1(String fromId,String toId){
        authorcnDao.deleteByNodeId(fromId,toId);
    }

    @Autowired
    Lend1Dao lend1Dao;
    public Lend1 addLend1(String from,String to,String time){
//        Lend1 lend1=new Lend1(from,to,time);
//        return lend1Dao.save(lend1);
        return lend1Dao.addLend(from, to, time);
    }
    public void deleteByNodeId11(String fromId,String toId){
        lend1Dao.deleteByNodeId(fromId,toId);
    }
    public Lend1 updateLend1(String from,String to,String time){
        return lend1Dao.updateLend(from,to,time);
    }


    @Autowired
    Authorof2Dao authorof2Dao;
    public Authorof2 addAuthor2(Fgbook from, People to){
        Authorof2 authorof2;
        authorof2 = new Authorof2(from, to);
        return authorof2Dao.save(authorof2);
    }
    public void deleteByNodeId2(String fromId,String toId){
        authorof2Dao.deleteByNodeId(fromId, toId);
    }


    @Autowired
    Lend2Dao lend2Dao;
    public Lend2 addLend2(String from,String to,String time){
//        Lend2 lend2=new Lend2(from,to,time);
//        return lend2Dao.save(lend2);
        return lend2Dao.addLend(from, to, time);
    }
    public void deleteByNodeId22(String fromId,String toId){
        lend2Dao.deleteByNodeId(fromId,toId);
    }
    public Lend2 updateLend2(String from,String to,String time){
        return lend2Dao.updateLend(from,to,time);
    }

    @Autowired
    Authorof3Dao authorof3Dao;
    public Authorof3 addAuthor3(Mscbook from, People to){
        Authorof3 authorof3=new Authorof3(from,to);
        return authorof3Dao.save(authorof3);
    }
    public void deleteByNodeId3(String fromId,String toId){
        authorof3Dao.deleteByNodeId(fromId, toId);
    }


    @Autowired
    Lend3Dao lend3Dao;
    public Lend3 addLend3(String from,String to,String time){
//        Lend3 lend3=new Lend3(from,to,time);
//        return lend3Dao.save(lend3);
        return lend3Dao.addLend(from, to, time);
    }
    public void deleteByNodeId33(String fromId,String toId){
        lend3Dao.deleteByNodeId(fromId,toId);
    }
    public Lend3 updateLend3(String from,String to,String time){
        return lend3Dao.updateLend(from,to,time);
    }

    @Autowired
    Authorof4Dao authorof4Dao;
    public Authorof4 addAuthor4(Mulbook from, People to){
        Authorof4 authorof4=new Authorof4(from,to);
        return authorof4Dao.save(authorof4);
    }
    public void deleteByNodeId4(String fromId,String toId){
        authorof4Dao.deleteByNodeId(fromId, toId);
    }



    @Autowired
    Lend4Dao lend4Dao;
    public Lend4 addLend4(String from, String to, String time){
//        Lend4 lend4=new Lend4(from,to,time);
//        return lend4Dao.save(lend4);
        return lend4Dao.addLend(from, to, time);
    }
    public void deleteByNodeId44(String fromId,String toId){
        lend4Dao.deleteByNodeId(fromId,toId);
    }
    public Lend4 updateLend4(String from,String to,String time){
        return lend4Dao.updateLend(from,to,time);
    }

    @Autowired
    Authorof5Dao authorof5Dao;
    public Authorof5 addAuthor5(Papbook from, People to){
        Authorof5 authorof5=new Authorof5(from,to);
        return authorof5Dao.save(authorof5);
    }
    public void deleteByNodeId5(String fromId,String toId){
        authorof5Dao.deleteByNodeId(fromId, toId);
    }


    @Autowired
    Lend5Dao lend5Dao;
    public Lend5 addLend5(String from, String to, String time){
//        Lend5 lend5=new Lend5(from,to,time);
//        return lend5Dao.save(lend5);
        return lend5Dao.addLend(from, to, time);
    }
    public void deleteByNodeId55(String fromId,String toId){
        lend5Dao.deleteByNodeId(fromId,toId);
    }
    public Lend5 updateLend5(String from,String to,String time){
        return lend5Dao.updateLend(from,to,time);
    }

    @Autowired
    Authorof6Dao authorof6Dao;
    public Authorof6 addAuthor6(Perbook from, People to){
        Authorof6 authorof6=new Authorof6(from,to);
        return authorof6Dao.save(authorof6);
    }
    public void deleteByNodeId6(String fromId,String toId){
        authorof6Dao.deleteByNodeId(fromId, toId);
    }


    @Autowired
    Lend6Dao lend6Dao;
    public Lend6 addLend6(String from, String to, String time){
//        Lend6 lend6=new Lend6(from,to,time);
//        return lend6Dao.save(lend6);
        return lend6Dao.addLend(from, to, time);
    }
    public void deleteByNodeId66(String fromId,String toId){
        lend6Dao.deleteByNodeId(fromId,toId);
    }
    public Lend6 updateLend6(String from,String to,String time){
        return lend6Dao.updateLend(from,to,time);
    }

}
